package org.jfinanceobserver.persistence.jpa.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.jfinanceobserver.persistence.jpa.controller.exceptions.NonexistentEntityException;
import org.jfinanceobserver.persistence.jpa.entities.QuoteProperty;

/**
 * This is QuotePropertyJpaController
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-11 13:03:43 +0000 (Mon, 11 Feb 2013) $ $LastChangedRevision: 2 $
 *
 * @since Feb 10, 2013
 */
public class QuotePropertyJpaController extends AbstractJpaController<QuoteProperty> implements Serializable {

    public QuotePropertyJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    @Override
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(QuoteProperty quoteProperty) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(quoteProperty);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(QuoteProperty quoteProperty) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            quoteProperty = em.merge(quoteProperty);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = quoteProperty.getId();
                if (findQuoteProperty(id) == null) {
                    throw new NonexistentEntityException("The quoteProperty with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuoteProperty quoteProperty;
            try {
                quoteProperty = em.getReference(QuoteProperty.class, id);
                quoteProperty.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The quoteProperty with id " + id + " no longer exists.", enfe);
            }
            em.remove(quoteProperty);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<QuoteProperty> findQuotePropertyEntities() {
        return findQuotePropertyEntities(true, -1, -1);
    }

    public List<QuoteProperty> findQuotePropertyEntities(int maxResults, int firstResult) {
        return findQuotePropertyEntities(false, maxResults, firstResult);
    }

    private List<QuoteProperty> findQuotePropertyEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(QuoteProperty.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public QuoteProperty findQuoteProperty(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(QuoteProperty.class, id);
        } finally {
            em.close();
        }
    }

    public int getQuotePropertyCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<QuoteProperty> rt = cq.from(QuoteProperty.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
